﻿using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.Serialization;

namespace HalconDotNet
{
	/// <summary>Represents an instance of a Deep Neural Network.</summary>
	// Token: 0x02000035 RID: 53
	[Serializable]
	public class HDlModel : HHandle, ISerializable, ICloneable
	{
		// Token: 0x060004C2 RID: 1218 RVA: 0x0000883B File Offset: 0x00006A3B
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HDlModel() : base(HHandleBase.UNDEF)
		{
		}

		// Token: 0x060004C3 RID: 1219 RVA: 0x0001C39E File Offset: 0x0001A59E
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HDlModel(IntPtr handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x060004C4 RID: 1220 RVA: 0x0001C3AD File Offset: 0x0001A5AD
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HDlModel(HHandle handle) : base(handle)
		{
			this.AssertSemType();
		}

		// Token: 0x060004C5 RID: 1221 RVA: 0x0001C3BC File Offset: 0x0001A5BC
		private void AssertSemType()
		{
			base.AssertSemType("dl_model");
		}

		// Token: 0x060004C6 RID: 1222 RVA: 0x0001C3C9 File Offset: 0x0001A5C9
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HDlModel obj)
		{
			obj = new HDlModel(HHandleBase.UNDEF);
			return obj.Load(proc, parIndex, err);
		}

		// Token: 0x060004C7 RID: 1223 RVA: 0x0001C3E4 File Offset: 0x0001A5E4
		internal static int LoadNew(IntPtr proc, int parIndex, int err, out HDlModel[] obj)
		{
			HTuple htuple;
			err = HTuple.LoadNew(proc, parIndex, err, out htuple);
			obj = new HDlModel[htuple.Length];
			for (int i = 0; i < htuple.Length; i++)
			{
				obj[i] = new HDlModel(HalconAPI.IsLegacyHandleMode() ? htuple[i].IP : htuple[i].H);
			}
			htuple.Dispose();
			return err;
		}

		/// <summary>
		///   Create a deep learning network for object detection.
		///   Modified instance represents: Deep learning model for object detection.
		/// </summary>
		/// <param name="backbone">Deep learning classifier, used as backbone network. Default: "pretrained_dl_classifier_compact.hdl"</param>
		/// <param name="numClasses">Number of classes. Default: 3</param>
		/// <param name="DLModelDetectionParam">Parameters for the object detection model. Default: []</param>
		// Token: 0x060004C8 RID: 1224 RVA: 0x0001C454 File Offset: 0x0001A654
		public HDlModel(string backbone, int numClasses, HDict DLModelDetectionParam)
		{
			IntPtr proc = HalconAPI.PreCall(2150);
			HalconAPI.StoreS(proc, 0, backbone);
			HalconAPI.StoreI(proc, 1, numClasses);
			HalconAPI.Store(proc, 2, DLModelDetectionParam);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(DLModelDetectionParam);
		}

		/// <summary>
		///   Read a deep learning model from a file.
		///   Modified instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="fileName">Filename Default: "pretrained_dl_classifier_compact.hdl"</param>
		// Token: 0x060004C9 RID: 1225 RVA: 0x0001C4B8 File Offset: 0x0001A6B8
		public HDlModel(string fileName)
		{
			IntPtr proc = HalconAPI.PreCall(2163);
			HalconAPI.StoreS(proc, 0, fileName);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		// Token: 0x060004CA RID: 1226 RVA: 0x0001C504 File Offset: 0x0001A704
		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			HSerializedItem hserializedItem = this.SerializeDlModel();
			byte[] value = hserializedItem;
			hserializedItem.Dispose();
			info.AddValue("data", value, typeof(byte[]));
		}

		// Token: 0x060004CB RID: 1227 RVA: 0x0001C53C File Offset: 0x0001A73C
		[EditorBrowsable(EditorBrowsableState.Never)]
		public HDlModel(SerializationInfo info, StreamingContext context)
		{
			HSerializedItem hserializedItem = new HSerializedItem((byte[])info.GetValue("data", typeof(byte[])));
			this.DeserializeDlModel(hserializedItem);
			hserializedItem.Dispose();
		}

		/// <summary>Serialize object to binary stream in HALCON format</summary>
		// Token: 0x060004CC RID: 1228 RVA: 0x0001C57C File Offset: 0x0001A77C
		public new void Serialize(Stream stream)
		{
			HSerializedItem hserializedItem = this.SerializeDlModel();
			hserializedItem.Serialize(stream);
			hserializedItem.Dispose();
		}

		/// <summary>Deserialize object from binary stream in HALCON format</summary>
		// Token: 0x060004CD RID: 1229 RVA: 0x0001C590 File Offset: 0x0001A790
		public new static HDlModel Deserialize(Stream stream)
		{
			HDlModel hdlModel = new HDlModel();
			HSerializedItem hserializedItem = HSerializedItem.Deserialize(stream);
			hdlModel.DeserializeDlModel(hserializedItem);
			hserializedItem.Dispose();
			return hdlModel;
		}

		// Token: 0x060004CE RID: 1230 RVA: 0x0001C5B6 File Offset: 0x0001A7B6
		object ICloneable.Clone()
		{
			return this.Clone();
		}

		// Token: 0x060004CF RID: 1231 RVA: 0x0001C5C0 File Offset: 0x0001A7C0
		public new HDlModel Clone()
		{
			HSerializedItem hserializedItem = this.SerializeDlModel();
			HDlModel hdlModel = new HDlModel();
			hdlModel.DeserializeDlModel(hserializedItem);
			hserializedItem.Dispose();
			return hdlModel;
		}

		/// <summary>
		///   Apply a deep-learning-based network on a set of images for inference.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="DLSampleBatch">Input data.</param>
		/// <param name="outputs">Requested outputs. Default: []</param>
		/// <returns>Handle containing result data.</returns>
		// Token: 0x060004D0 RID: 1232 RVA: 0x0001C5E8 File Offset: 0x0001A7E8
		public HDict[] ApplyDlModel(HDict[] DLSampleBatch, HTuple outputs)
		{
			HTuple htuple = HHandleBase.ConcatArray(DLSampleBatch);
			IntPtr proc = HalconAPI.PreCall(2146);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, htuple);
			HalconAPI.Store(proc, 2, outputs);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HalconAPI.UnpinTuple(outputs);
			HDict[] result;
			num = HDict.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(DLSampleBatch);
			return result;
		}

		/// <summary>Clear a deep learning model.</summary>
		/// <param name="DLModelHandle">Handle of the deep learning model.</param>
		// Token: 0x060004D1 RID: 1233 RVA: 0x0001C660 File Offset: 0x0001A860
		public static void ClearDlModel(HDlModel[] DLModelHandle)
		{
			HTuple htuple = HHandleBase.ConcatArray(DLModelHandle);
			IntPtr proc = HalconAPI.PreCall(2147);
			HalconAPI.Store(proc, 0, htuple);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(DLModelHandle);
		}

		/// <summary>
		///   Clear a deep learning model.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		// Token: 0x060004D2 RID: 1234 RVA: 0x0001C6A4 File Offset: 0x0001A8A4
		public void ClearDlModel()
		{
			IntPtr proc = HalconAPI.PreCall(2147);
			base.Store(proc, 0);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Create a deep learning network for object detection.
		///   Modified instance represents: Deep learning model for object detection.
		/// </summary>
		/// <param name="backbone">Deep learning classifier, used as backbone network. Default: "pretrained_dl_classifier_compact.hdl"</param>
		/// <param name="numClasses">Number of classes. Default: 3</param>
		/// <param name="DLModelDetectionParam">Parameters for the object detection model. Default: []</param>
		// Token: 0x060004D3 RID: 1235 RVA: 0x0001C6D8 File Offset: 0x0001A8D8
		public void CreateDlModelDetection(string backbone, int numClasses, HDict DLModelDetectionParam)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(2150);
			HalconAPI.StoreS(proc, 0, backbone);
			HalconAPI.StoreI(proc, 1, numClasses);
			HalconAPI.Store(proc, 2, DLModelDetectionParam);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(DLModelDetectionParam);
		}

		/// <summary>
		///   Deserialize a deep learning model.
		///   Modified instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="serializedItemHandle">Handle of the serialized item.</param>
		// Token: 0x060004D4 RID: 1236 RVA: 0x0001C73C File Offset: 0x0001A93C
		public void DeserializeDlModel(HSerializedItem serializedItemHandle)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(2151);
			HalconAPI.Store(proc, 0, serializedItemHandle);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(serializedItemHandle);
		}

		/// <summary>
		///   Return the parameters of a deep learning model.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="genParamName">Name of the generic parameter. Default: "batch_size"</param>
		/// <returns>Value of the generic parameter.</returns>
		// Token: 0x060004D5 RID: 1237 RVA: 0x0001C790 File Offset: 0x0001A990
		public HTuple GetDlModelParam(string genParamName)
		{
			IntPtr proc = HalconAPI.PreCall(2156);
			base.Store(proc, 0);
			HalconAPI.StoreS(proc, 1, genParamName);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HTuple result;
			num = HTuple.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Read a deep learning model from a file.
		///   Modified instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="fileName">Filename Default: "pretrained_dl_classifier_compact.hdl"</param>
		// Token: 0x060004D6 RID: 1238 RVA: 0x0001C7E0 File Offset: 0x0001A9E0
		public void ReadDlModel(string fileName)
		{
			this.Dispose();
			IntPtr proc = HalconAPI.PreCall(2163);
			HalconAPI.StoreS(proc, 0, fileName);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			num = base.Load(proc, 0, num);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Serialize a deep learning model.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		/// <returns>Handle of the serialized item.</returns>
		// Token: 0x060004D7 RID: 1239 RVA: 0x0001C82C File Offset: 0x0001AA2C
		public HSerializedItem SerializeDlModel()
		{
			IntPtr proc = HalconAPI.PreCall(2168);
			base.Store(proc, 0);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HSerializedItem result;
			num = HSerializedItem.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			return result;
		}

		/// <summary>
		///   Set the parameters of a deep learning model.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="genParamName">Name of the generic parameter. Default: "learning_rate"</param>
		/// <param name="genParamValue">Value of the generic parameter. Default: 0.001</param>
		// Token: 0x060004D8 RID: 1240 RVA: 0x0001C874 File Offset: 0x0001AA74
		public void SetDlModelParam(string genParamName, HTuple genParamValue)
		{
			IntPtr proc = HalconAPI.PreCall(2171);
			base.Store(proc, 0);
			HalconAPI.StoreS(proc, 1, genParamName);
			HalconAPI.Store(proc, 2, genParamValue);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(genParamValue);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Set the parameters of a deep learning model.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="genParamName">Name of the generic parameter. Default: "learning_rate"</param>
		/// <param name="genParamValue">Value of the generic parameter. Default: 0.001</param>
		// Token: 0x060004D9 RID: 1241 RVA: 0x0001C8C0 File Offset: 0x0001AAC0
		public void SetDlModelParam(string genParamName, double genParamValue)
		{
			IntPtr proc = HalconAPI.PreCall(2171);
			base.Store(proc, 0);
			HalconAPI.StoreS(proc, 1, genParamName);
			HalconAPI.StoreD(proc, 2, genParamValue);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Train a deep learning model.
		///   Instance represents: Deep learning model handle.
		/// </summary>
		/// <param name="DLSampleBatch">Tuple of Dictionaries with input images and corresponding information.</param>
		/// <returns>Dictionary with the train result data.</returns>
		// Token: 0x060004DA RID: 1242 RVA: 0x0001C904 File Offset: 0x0001AB04
		public HDict TrainDlModelBatch(HDict[] DLSampleBatch)
		{
			HTuple htuple = HHandleBase.ConcatArray(DLSampleBatch);
			IntPtr proc = HalconAPI.PreCall(2172);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, htuple);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HDict result;
			num = HDict.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(DLSampleBatch);
			return result;
		}

		/// <summary>
		///   Write a deep learning model in a file.
		///   Instance represents: Handle of the deep learning model.
		/// </summary>
		/// <param name="fileName">Filename</param>
		// Token: 0x060004DB RID: 1243 RVA: 0x0001C96C File Offset: 0x0001AB6C
		public void WriteDlModel(string fileName)
		{
			IntPtr proc = HalconAPI.PreCall(2174);
			base.Store(proc, 0);
			HalconAPI.StoreS(proc, 1, fileName);
			int procResult = HalconAPI.CallProcedure(proc);
			HalconAPI.PostCall(proc, procResult);
			GC.KeepAlive(this);
		}

		/// <summary>
		///   Infer the sample and generate a heatmap.
		///   Instance represents: Handle of a Deep learning model.
		/// </summary>
		/// <param name="DLSample">Dictionaries with the sample input data.</param>
		/// <param name="heatmapMethod">Method to be used for the heatmap calculation. Default: "grad_cam"</param>
		/// <param name="targetClasses">ID of the target class. Default: []</param>
		/// <param name="genParam">Dictionary for generic parameters. Default: []</param>
		/// <returns>Dicitonaries with the result data.</returns>
		// Token: 0x060004DC RID: 1244 RVA: 0x0001C9A8 File Offset: 0x0001ABA8
		public HDict[] GenDlModelHeatmap(HDict[] DLSample, string heatmapMethod, HTuple targetClasses, HDict genParam)
		{
			HTuple htuple = HHandleBase.ConcatArray(DLSample);
			IntPtr proc = HalconAPI.PreCall(2184);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, htuple);
			HalconAPI.StoreS(proc, 2, heatmapMethod);
			HalconAPI.Store(proc, 3, targetClasses);
			HalconAPI.Store(proc, 4, genParam);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HalconAPI.UnpinTuple(targetClasses);
			HDict[] result;
			num = HDict.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(DLSample);
			GC.KeepAlive(genParam);
			return result;
		}

		/// <summary>
		///   Train a deep learning model for anomaly detection.
		///   Instance represents: Deep learning model handle.
		/// </summary>
		/// <param name="DLSamples">Tuple of Dictionaries with input images and corresponding information.</param>
		/// <param name="DLTrainParam">Parameter for training the anomaly detection model. Default: []</param>
		/// <returns>Dictionary with the train result data.</returns>
		// Token: 0x060004DD RID: 1245 RVA: 0x0001CA38 File Offset: 0x0001AC38
		public HDict TrainDlModelAnomalyDataset(HDict[] DLSamples, HDict DLTrainParam)
		{
			HTuple htuple = HHandleBase.ConcatArray(DLSamples);
			IntPtr proc = HalconAPI.PreCall(2189);
			base.Store(proc, 0);
			HalconAPI.Store(proc, 1, htuple);
			HalconAPI.Store(proc, 2, DLTrainParam);
			HalconAPI.InitOCT(proc, 0);
			int num = HalconAPI.CallProcedure(proc);
			HalconAPI.UnpinTuple(htuple);
			HDict result;
			num = HDict.LoadNew(proc, 0, num, out result);
			HalconAPI.PostCall(proc, num);
			GC.KeepAlive(this);
			GC.KeepAlive(DLSamples);
			GC.KeepAlive(DLTrainParam);
			return result;
		}
	}
}
